package com.his.drugstage.controller;

import com.his.common.Result;
import com.his.drugstage.pojo.DrugInfo;
import com.his.drugstage.pojo.Prescription;
import com.his.drugstage.pojo.Register;
import com.his.drugstage.service.DrugstageService;
import com.his.drugstage.service.DrugPrescriptionService;
import com.his.drugstage.service.DrugRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;


@RestController//标记此类为控制层组件

@CrossOrigin("*")/*1、解决跨域访问，如咱们前后端分离项目中前端使用axios获取数据，后端控制层需要加此注解
                   2、*代表允许所有的url访问                        */

@RequestMapping("drugInfo")/*1、处理请求和响应
                             2、将方法与路径映射到一起
                             3、来标识Controller类或方法要处理的URL路径
                             注，如果省略了该注解，Spring将无法确定请求应该由哪个Controller来处理*/

public class DrugstageController {
    @Autowired//自动注入服务组件  此处使用@AutoWired注解也可以
    private DrugstageService drugstageService;
    @Autowired
    private DrugPrescriptionService drugPrescriptionService;
    @Autowired
    private DrugRegisterService drugRegisterService;

    @GetMapping("query")//将方法映射到HTTP GET请求的URL路径,但只处理GET请求。使用postman测试时可以使用/query路径来测试接口
    public Result queryAllDrugInfo(){//查询所有药品信息
        ArrayList<DrugInfo> list= drugstageService.queryAllDrugInfo();
        if(list!=null){
            return new Result(1,"查询药品信息列表成功！",list);
        }
        else{
            return new Result(0,"查询药品信息列表失败！",null);
        }
    }
    @PostMapping("add")//将方法映射到HTTP POST请求的URL路径,但只处理POST请求
    public Result addDrugInfo(@RequestBody DrugInfo drugInfo){//因为是post请求，需要接收前端返回的数据，所以此处的参数前面要加注解@RequestBody，这样才能接收前端传过来的Json格式的对象或数组（如果是数组，后端可用集合来接收）
        int result= drugstageService.addDrugInfo(drugInfo);
        if(result>0){
            return new Result(1,"添加药品信息成功！",null);
        }
        else{
            return new Result(0,"添加药品信息失败！",null);
        }
    }
    @PostMapping("delete")
    public Result deleteDrugInfoById(@RequestBody DrugInfo  drugInfo){
        int result= drugstageService.deleteDrugInfoById(drugInfo);
        if(result>0){
            return new Result(1,"删除药品信息成功！",null);
        }
        else{
            return new Result(0,"删除药品信息失败！",null);
        }
    }

    @RequestMapping("update")
    public Result updateDrugInfoById(@RequestBody DrugInfo drugInfo){
        int result= drugstageService.updateDrugInfoById(drugInfo);
        if(result>0){
            return new Result(1,"更新药品信息成功！",null);
        }
        else{
            return new Result(0,"更新药品信息失败！",null);
        }
    }

    @PostMapping("select")
    public Result queryDrugInfoByConditions(@RequestBody DrugInfo drugInfo){
        ArrayList<DrugInfo> list= drugstageService.queryDrugInfoByConditions(drugInfo);
        if(list!=null){
            return new Result(1,"条件查询药品信息成功！",list);
        }
        else{
            return new Result(0,"条件查询药品信息失败！",null);
        }
    }

    @PostMapping("selectRegister")
    public Result queryRegisterByConditions(@RequestBody Register register){
        ArrayList<Register> list= drugRegisterService.queryRegisterByConditions(register);
        if(list!=null){
            return new Result(1,"查询患者信息成功！",list);
        }
        else{
            return new Result(0,"查询患者信息失败！",null);
        }
    }

    @PostMapping("selectPrescriptionDrugInfo")
    public Result selectPrescriptionDrugInfo(@RequestBody Register register){
        ArrayList<DrugInfo> list= drugPrescriptionService.selectPrescriptionDrugInfo(register);
        if(list!=null){
            return new Result(1,"查询药品信息成功！",list);
        }
        else{
            return new Result(0,"查询药品信息失败！",null);
        }
    }
    @PostMapping("updateDrugState")
    public Result updateDrugState(@RequestBody Prescription prescription){
        int result= drugPrescriptionService.updateDrugState(prescription);
        if(result>0){
            return new Result(1,"更新处方药品状态成功！",result);
        }
        else{
            return new Result(0,"更新处方药品状态失败！",null);
        }
    }

}

